package com.my.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.entity.WecomRobot;
import com.my.mapper.WecomRobotMapper;
import com.my.request.WecomGroupAddReq;
import com.my.request.WecomGroupChangeReq;
import com.my.request.WecomGroupQueryReq;
import com.my.request.WecomGroupUpdateReq;
import com.my.response.WecomGroupDetailResponse;
import com.my.response.WecomGroupResponse;
import com.my.result.PageResult;
import com.my.service.WecomRobotService;
import com.my.util.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjx
 * @date 2025/2/10 13:58
 */
@Service
public class WecomRobotServiceImpl extends ServiceImpl<WecomRobotMapper, WecomRobot> implements WecomRobotService {

    @Override
    public PageResult<WecomGroupResponse> getPage(WecomGroupQueryReq query) {
        Page<WecomRobot> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(query.getChatId()),WecomRobot::getChatId,query.getChatId())
                .like(StringUtils.isNotBlank(query.getChatName()),WecomRobot::getName,query.getChatName())
                .eq(query.getStatus() != null,WecomRobot::getStatus,query.getStatus())
                .ge(query.getBeginTime() != null,WecomRobot::getCreateTime,query.getBeginTime())
                .le(query.getEndTime() != null,WecomRobot::getCreateTime,query.getEndTime())
                .page(new Page<>(query.getCurrent(),query.getPageSize()));
        if(CollectionUtils.isEmpty(page.getRecords())){
            return PageResultUtil.buildPageInfo(new ArrayList<>(),page.getTotal());
        }

        List<WecomGroupResponse> responseList = new ArrayList<>();
        for(WecomRobot wecomRobot : page.getRecords()){
            WecomGroupResponse wecomGroupResponse = new WecomGroupResponse();
            wecomGroupResponse.setId(wecomRobot.getId());
            wecomGroupResponse.setChatId(wecomRobot.getChatId());
            wecomGroupResponse.setChatName(wecomRobot.getName());
            wecomGroupResponse.setRobotWebhook(wecomRobot.getRobotWebhook());
            wecomGroupResponse.setStatus(wecomRobot.getStatus());
            wecomGroupResponse.setCreateTime(wecomRobot.getCreateTime());
            responseList.add(wecomGroupResponse);
        }

        return PageResultUtil.buildPageInfo(responseList,page.getTotal());
    }

    @Override
    public WecomGroupDetailResponse getInfo(Long id) {
        WecomRobot wecomRobot = this.getById(id);
        if(wecomRobot == null){
            return null;
        }
        WecomGroupDetailResponse wecomGroupResponse = new WecomGroupDetailResponse();
        wecomGroupResponse.setId(wecomRobot.getId());
        wecomGroupResponse.setChatId(wecomRobot.getChatId());
        wecomGroupResponse.setChatName(wecomRobot.getName());
        wecomGroupResponse.setRobotWebhook(wecomRobot.getRobotWebhook());
        wecomGroupResponse.setRemark(wecomRobot.getRemark());
        return wecomGroupResponse;
    }

    @Override
    public WecomGroupDetailResponse getInfoByChatId(String chatId) {
        WecomRobot wecomRobot = this.lambdaQuery()
                .eq(WecomRobot::getChatId,chatId)
                .last("limit 1")
                .one();
        if(wecomRobot == null){
            return null;
        }
        WecomGroupDetailResponse wecomGroupResponse = new WecomGroupDetailResponse();
        wecomGroupResponse.setId(wecomRobot.getId());
        wecomGroupResponse.setChatId(wecomRobot.getChatId());
        wecomGroupResponse.setChatName(wecomRobot.getName());
        wecomGroupResponse.setRobotWebhook(wecomRobot.getRobotWebhook());
        wecomGroupResponse.setRemark(wecomRobot.getRemark());
        wecomGroupResponse.setStatus(wecomRobot.getStatus());
        return wecomGroupResponse;
    }

    @Override
    public void addInfo(WecomGroupAddReq request) {
        WecomRobot wecomRobot = new WecomRobot();
        wecomRobot.setChatId(request.getChatId());
        wecomRobot.setName(request.getChatName());

        Boolean robotWebhookFlag = Boolean.FALSE;
        if(StringUtils.isNotBlank(request.getRobotWebhook())){
            wecomRobot.setRobotWebhook(request.getRobotWebhook());
            robotWebhookFlag = Boolean.TRUE;
        }
        wecomRobot.setRobotWebhookFlag(robotWebhookFlag);
        wecomRobot.setRemark(request.getRemark());
        this.save(wecomRobot);
    }

    @Override
    public void updateInfo(WecomGroupUpdateReq request) {
        WecomRobot wecomRobot = new WecomRobot();
        wecomRobot.setId(request.getId());
        wecomRobot.setName(request.getChatName());

        if(StringUtils.isNotBlank(request.getRobotWebhook())){
            wecomRobot.setRobotWebhook(request.getRobotWebhook());
            wecomRobot.setRobotWebhookFlag(Boolean.TRUE);
        }

        wecomRobot.setRemark(request.getRemark());
        this.updateById(wecomRobot);
    }

    @Override
    public void delById(Long id) {
        this.removeById(id);
    }

    @Override
    public void changeStatus(WecomGroupChangeReq request) {
        WecomRobot wecomRobot = new WecomRobot();
        wecomRobot.setId(request.getId());
        wecomRobot.setStatus(request.getStatus());
        this.updateById(wecomRobot);
    }
}
